package com.bw.shop2208a.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import com.bw.shop2208a.entity.LoginParam
import com.bw.shop2208a.entity.RegisterParam
import com.bw.shop2208a.entity.UserResult
import com.bw.shop2208a.mvvm.BaseViewModel
import com.bw.shop2208a.mvvm.SingleLiveData
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch

class UserViewModel :BaseViewModel() {
    val success_login = MutableLiveData<UserResult>()
    val fail_login = MutableLiveData<String>()

    fun login(loginParam: LoginParam){
        viewModelScope.launch {//作用域
            try {//收到添加报错的可能
                success_login.value = apiservice.login(loginParam)
            }catch (e : Exception){
                fail_login.value = e.message
            }
        }
    }

    val success_register = MutableLiveData<UserResult>()
    val fail_register = MutableLiveData<String>()

    fun register(registerParam: RegisterParam){
        viewModelScope.launch {
            try {
                success_register.value = apiservice.register(registerParam)
            } catch (e: Exception) {
                fail_register.value = e.message
            }
        }
    }
}